<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!--将要添加小班学生列表弹出框-->
<div class="row">
	<div class="col-md-2 col-lg-2"></div>
	<div class="col-md-8 col-lg-8">
		<div class="col-md-12">
			<div class="form-group">
				<div class="row">
					<div class="col-md-12 col-lg-12" style="margin-top:3rem;text-align:center;">
						<label for="">添加学生</label>
					</div>
				</div>
				<div class="row" style="margin-top:2rem;">
					<div class="col-md-6 col-lg-6">
						<input type="text" name="searchSmallStudent" class="form-control" id="" placeholder="学号/姓名">
					</div>
					<div class="col-md-3 col-lg-3">
						<button class="btn btn-primary" onclick="seachSmallStudentEvent(1)" id="searchSmallStudent">搜索</button>
					</div>
				</div>
			</div>
		</div>
		
		<div class="col-md-12">
			<table class="table table-hover">
				<thead>
					<tr>
						<th>序号</th>
						<th>学号</th>
						<th>姓名</th>
						<th><input type="checkbox" name="" id="checkAddSmall" value="" />全选</th>
					</tr>
				</thead>
				<tbody  id="searchSmallStudentList">
					
				</tbody>
			</table>
		</div>
		
		<div class="col-md-6" style="text-align:left;">
			<button type="button" id="batchAddSmallStudentToClass" class="btn btn-primary">添加</button>
		</div>
		<div class="col-md-6">
			<div class="page-content" id="classAddPage">
				<nav class="pagination" role="navigation" style="text-align:right;">
					<a class="last-page" id="classAddPage-last-page">
						<i class="fa fa-angle-left"></i>
					</a>
					<span class="page-number" id="classAddPage-page-number">/</span>
					<a class="next-page" id="classAddPage-next-page">
						<i class="fa fa-angle-right"></i>
					</a>
				</nav>
			</div>
		</div>
	</div>
	<div class="col-md-2 col-lg-2"></div>
</div>
<script>
$(function() {
	$("#batchAddSmallStudentToClass").click(batchAddSmallStudentToClass);
	$("#checkAddSmall").click(function(){
		//全选状态
		if($("#checkAddSmall").is(':checked')){
			$('input[name="smallStudentId"]').each(function(){
				$(this).prop("checked", true);
			});
		}else{//全不选状态
			$('input[name="smallStudentId"]').each(function(){
				$(this).prop("checked", false);
			});
		}
	});
});
var getCSmallStudents = function(pageNo){
	var id=$("input[name='classId']").val();
	var json={
		classId:id,
		pageNo:pageNo
	};
	
	$.get(CONTEXT_PATH+"cscontrol/ajaxListData",{
		data : JSON.stringify(json)
	},function(data) {
		if(data.status=="success"){
			var searchSmallStudentList = $("#searchSmallStudentList");
			searchSmallStudentList.empty();
    		var s="";
    		var students=data.data.list;
    		if(students.length <= 0){
    			s += "<tr><td colspan=\"4\">无结果</td></tr>";
    		}else{
    			for(var i=0;i<students.length;i++){
        			var item=students[i];
        			s+="<tr>";
        			s+="<td scope=\"row\">"+(i+1)+"</td>";
        			s+="<td title=\""+item.studentCode+"\">"+item.studentCode+"</td>";
        			s+="<td title=\""+item.name+"\">"+item.name+"</td>";
        			s+="<td><input type=\"checkbox\" name=\"smallStudentId\" value=\""+item.id+"\" /></td>";
        			s+="</tr>"
        		}
    		}
    		
    		searchSmallStudentList.append(s);//initPages
    		addSmallStuPages(data.data.pageNo,data.data.allpages);
		}else{
			layer.alert(data.message);
		}
	}); 
}

var addSmallStuPages = function(pageNo,allpages){
	var lastPageObj=$("#classAddPage-last-page");
	var nextPageObj=$("#classAddPage-next-page");
	var pageNumberObj=$("#classAddPage-page-number");
	var lastPage=pageNo-1;
	if(lastPage<=0){//设置disableed
		lastPage=1;
	}else{
		$("#classAddPage-last-page").bind("click",{pageNo:lastPage},addSmallPage);
	};
	var nextPage = pageNo+1;
	if(nextPage>allpages){
		nextPage=allpages;
	}else{
		$("#classAddPage-next-page").bind("click",{pageNo:nextPage}, addSmallPage);
	}
	pageNumberObj.text("第"+pageNo+"页/共"+allpages+"页");
}
var addSmallPage = function(event){
	var pageNo=event.data.pageNo;
	seachSmallStudentEvent(pageNo);
}

var seachSmallStudentEvent = function(pageNo){
	if(isEmpty(pageNo)){
		pageNo=1;
	}
	var keywords=$("input[name='searchSmallStudent']").val();
	var classId=$("input[name='classId']").val();
	if(keywords==""){
		getCSmallStudents(pageNo);
		return;
	}
	var json={
			keywords:keywords,
			classId:classId,
			pageNo:pageNo
	};
	$.get(CONTEXT_PATH+"cscontrol/searchListData", {
		data : JSON.stringify(json)
	} ,function(data) {
		if(data.status=="success"){
			var searchSmallStudentList = $("#searchSmallStudentList");
			searchSmallStudentList.empty();
    		var s="";
    		var students=data.data.list;
    		for(var i=0;i<students.length;i++){
    			var item=students[i];
    			s+="<tr>";
    			s+="<th scope=\"row\">"+(i+1)+"</th>";
    			s+="<td title=\""+item.studentCode+"\">"+item.studentCode+"</td>";
    			s+="<td title=\""+item.name+"\">"+item.name+"</td>";
    			s+="<td><input type=\"checkbox\" name=\"smallStudentId\" value=\""+item.id+"\" /></td>";
    			s+="</tr>"
    		}
    		searchSmallStudentList.append(s);
    		addSmallStuPages(data.data.pageNo,data.data.allpages);
		}else{
			layer.alert(data.message);
		}
	}); 
}
var batchAddSmallStudentToClass = function (){
	var chk_value =[];
	$('input[name="smallStudentId"]:checked').each(function(){ 
		chk_value.push($(this).val()); 
	});
	if(chk_value.length<=0){
		layer.alert("请选择学生");
		return ;
	}
	var smallStudentId=JSON.stringify(chk_value);
	var classId=$("input[name='classId']").val();
	var json={
		"classId":classId,
		"studentId":chk_value
	}
	$.post(CONTEXT_PATH+"cscontrol/ajaxBatchCreateSmallClass", {
		data : JSON.stringify(json)
	}, function(data) {
		
		if(data.status=="success"){
			getCSmallStudents(1);
			setTimeout(function(){
				layer.closeAll();
			},300);
			getSmallStudentList(classId);
		}else{
			layer.alert("wrong");
		}
	}); 
	
}
</script>